<template>
  <div>
    <el-dialog title="添加用药信息" :visible.sync="dialogFormVisible">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>园地名称</span>
        </div>
        <el-form
        :model="form"
        v-loading="fetchLoading"
        class="sectionForm"
        size="mini"
        element-loading-text="数据加载中,请耐心等待..."
        label-width="80px"
        label-position="right">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="园地名称">
                  <el-select v-model="form.gardenPlot" placeholder="园地名称" value-key="gardenId">
                    <el-option v-for="item in gardenPlotList"
                      :key="item.value"
                      :label="item.gardenName"
                      :value="item"
                    />
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="园地面积">
                  <el-input v-model="form.gardenPlot.gardenArea" disabled placeholder="园地面积"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="种植品种">
                  <el-select v-model="form.gardenPlot.gardenVar" disabled placeholder="种植品种">
                    <el-option label="苹果" value="apple"></el-option>
                    <el-option label="香蕉" value="banana"></el-option>
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="果树数量">
                  <el-input v-model="form.gardenPlot.gardenSnum" disabled placeholder="果树数量"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>农药信息</span>
        </div>
        <el-form
        :model="form"
        v-loading="fetchLoading"
        class="sectionForm"
        size="mini"
        element-loading-text="数据加载中,请耐心等待..."
        label-width="80px"
        label-position="right">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="农药名称">
                  <el-select v-model="form.pesticidesBuy" placeholder="农药名称" value-key="pesticidesId">
                    <el-option v-for="item in pesticidesBuyList"
                      :key="item.value"
                      :label="item.pesticidesName"
                      :value="item"/>
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买序号">
                  <el-input v-model="form.pesticidesBuy.pesticidesSerialNum" disabled placeholder="购买序号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="农药编号">
                  <el-input v-model="form.pesticidesBuy.pesticidesNum" disabled placeholder="农药编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买单价">
                  <el-input v-model="form.pesticidesBuy.pesticidesUnitPrice" disabled placeholder="购买单价"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="购买总量">
                  <el-input v-model="form.pesticidesBuy.pesticidesQuantity" disabled placeholder="购买总量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买总额">
                  <el-input v-model="form.pesticidesBuy.pesticidesTotalPrice" disabled placeholder="购买总额"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="余存数量">
                  <el-input v-model="form.pesticidesBuy.count" disabled placeholder="余存数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="余存总额">
                  <el-input v-model="form.pesticidesBuy.countPric" disabled placeholder="余存总额"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>用药信息</span>
        </div>
        <el-form
        :model="form"
        v-loading="fetchLoading"
        class="sectionForm"
        size="mini"
        element-loading-text="数据加载中,请耐心等待..."
        label-width="80px"
        label-position="right">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="用药数量">
                  <el-input v-model="form.medQuantity"  @blur="change" placeholder="用药数量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用药总额">
                  <el-input v-model="form.medTotalPrice" disabled placeholder="用药总额"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="24">
              <el-form-item label="用药日期">
                 <el-date-picker v-model="form.medDate" type="date" placeholder="用药日期"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="type==='add'" @click="add">确认添加</el-button>
        <el-button type="primary" v-else @click="add">确认修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default{
  name:'add',
  props:{
    type:{
      type:String,
    }
  },
  data(){
    return {
      dialogFormVisible:false,
      fetchLoading: false,
      form: {
        gardenPlot: {
          gardenName: '',
          gardenArea: '',
          gardenVar: '',
          gardenSnum: '',
        },
        pesticidesBuy:{
          pesticidesSerialNum: '',
          pesticidesNum: '',
          pesticidesName: '',
          pesticidesUnitPrice: '',
          pesticidesQuantity: '',
          pesticidesTotalPrice: '',
          count: '',
          countPric:''
        },
        medQuantity: '',
        medTotalPrice: '',
        medDate: '',
        remark: '',
      },
      gardenPlotList: [],
      pesticidesBuyList: []
    }
  },
  async created(){
    let res=await this.$API.gardenInfo.getList({})
    if(res.code==200){
      this.gardenPlotList = res.data
      // console.log(this.gardenPlotList,5555)
    }
    let res1=await this.$API.pesticiedePur.getList({})
    if(res1.code==200){
      this.pesticidesBuyList = res1.data
    }
  },
  methods:{
    change(){
      if(this.form.medQuantity > this.form.pesticidesBuy.count){
         return this.$message({type:"error",message:"余存数量不足"})
      }
      this.form.medTotalPrice = this.form.medQuantity * this.form.pesticidesBuy.pesticidesUnitPrice 
    },
    cancel(){
      this.dialogFormVisible = false
      Object.assign(this.$data.form, this.$options.data().form)
    },
    async add(){
      this.dialogFormVisible = false
      // this.$emit("save",)
      try{
        let res=await this.$API.pharmacyInfo.saveOrUpdate(this.form)
        if(res.code==200){
          this.$message({type:"success",message:this.type==="add"?"添加成功":"修改成功"})
          this.$parent.updatePageList()
          Object.assign(this.$data.form, this.$options.data().form)
        }
      }catch{
        this.$message({type:"error",message:this.type==="add"?"添加失败":"修改失败"})
      }
    }
    
  }
}
</script>

<style scoped>
.text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 100%;
    margin-bottom: 20px;
  }
</style>